#include <jni.h>
#include <stdio.h>
#include <memory.h>
#include <stdlib.h>
#include <gsl/gsl_matrix.h>

#include "HelloWorld.h"
#define LLEN 10
#define X 10
#define Y 10


struct tree {
	int index;
	gsl_matrix *matrix;
	struct tree *left;
	struct tree *right;
};

void insert(struct tree **root, struct tree *item)
{
	if (!(*root))
	{
		*root = item;
		return;
	}
//       	printf("item = %d\n",item->a); 
	if (item->index < (*root)->index)
		insert(&(*root)->left, item);
	else
		if(item->index > (*root)-> index)
			insert(&(*root)->right,item);

return;
}


void printout(struct tree *root)
{
	int k,l;
	if (root->left) printout(root->left);
	for(k=0;k<X;k++)
	{
		for(l=0;l<Y;l++)
			printf("%g\t",gsl_matrix_get(root->matrix,k,l));
	printf("\n\n\n");
	}
	if (root->right) printout(root->right);
return;
}


JNIEXPORT void JNICALL
Java_HelloWorld_print(JNIEnv *en, jobject obj,jint dept)
{
	printf("Hello World\n");

struct tree *root, *temp;
int i,j,k,l;
root = NULL;
for(i=0;i<dept;i++)
for (j=0;j<dept;j++)
{
	
	temp = (struct tree *)malloc(sizeof(struct tree));
	temp->left = temp->right=NULL;
	temp->matrix  = gsl_matrix_alloc(X,Y);
	temp->index=rand();
	for(k=0;k<X;k++)
		for(l=0;l<Y;l++)
			gsl_matrix_set(temp->matrix,k,l,temp->index);
	insert(&root,temp);
}


printout(root);
}
